package com.njtech.pet.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.njtech.pet.constant.Constant;
import com.njtech.pet.entity.Bill;
import com.njtech.pet.entity.Item;
import com.njtech.pet.entity.User;
import com.njtech.pet.service.BillService;
import com.njtech.pet.service.CreditService;
import com.njtech.pet.service.ItemService;
import com.njtech.pet.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by m1870 on 2020/3/25.
 */
@Controller
@RequestMapping("/credit")
public class CreditController {
    @Autowired
    CreditService creditService;
    @Autowired
    ItemService itemService;
    @Autowired
    UserService userService;
    @Autowired
    BillService billService;


    /**
     * 显示总积分
     *
     * @param uid 用户id
     * @return
     */

    @RequestMapping("/showCredit")
    @ResponseBody
    public String showCredit(@RequestParam Integer uid) {

        Map<String, Object> resultMap = new HashMap<>();

        String json;

        Integer credit = creditService.getUserCredit(uid);

        resultMap.put("message", credit);

        json = JSON.toJSONString(resultMap);
        return json;
    }

    /**
     * 显示积分详情
     *
     * @param uid 用户id
     * @return 展示积分的bill list
     */

    @RequestMapping("/showCreditDetail")
    @ResponseBody
    public String showCreditDetail(@RequestParam Integer uid) {

        Map<String, Object> resultMap = new HashMap<>();

        User user = userService.selectByUserId(uid);

        List<Bill> list = creditService.getUserCreditHistory(user);

        resultMap.put("message", list);//积分详情

        resultMap= JSONObject.parseObject(JSON.toJSONString(resultMap, SerializerFeature.DisableCircularReferenceDetect));

        return JSON.toJSONString(resultMap);
    }



    /**
     * 复制链接
     *
     * @param uid    用户id
     * @param itemId 商品id
     * @return 商品链接
     */
    @RequestMapping("/copy")
    @ResponseBody
    public String doCopy(@RequestParam Integer uid, @RequestParam Integer itemId) {

        User user = userService.selectByUserId(uid);

        Item item = itemService.selectById(itemId);

        String itemUrl = creditService.copyLinks(user, item);

        Map<String, Object> resultMap = new HashMap<>();

        resultMap.put("message", itemUrl);

        return JSON.toJSONString(resultMap);
    }

    /**
     * 积分管理员部分
     */

    /**
     * 添加积分
     * @param uid 用户id
     * @param inputCredit 输入的积分
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public String addByAdmin(@RequestParam Integer uid, @RequestParam int inputCredit) {
        User user = userService.selectByUserId(uid);

        Map<String, Object> resultMap = new HashMap<>();

        if(user==null){
            resultMap.put("message","用户不存在");
        }else {
            creditService.addByAdmin(user, inputCredit);
            resultMap.put("message", "添加成功");
        }
        return JSON.toJSONString(resultMap);
    }


    /**
     * 删除积分
     * @param uid 用户
     * @param deleteCredit 需要删除的值
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteByAdmin(@RequestParam Integer uid, @RequestParam int deleteCredit) {
        User user = userService.selectByUserId(uid);

        Map<String, Object> resultMap = new HashMap<>();

        if(user.credit<deleteCredit){
            resultMap.put("message","删除积分大于用户积分");
        }
        else {
            creditService.deleteByAdmin(user, deleteCredit);

            resultMap.put("message", "已删除");
        }
        return JSON.toJSONString(resultMap);

    }
}
